﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperJoe.Framework.UnitOfWorkSql;
using WWX.Admin.IDAL;
using WWX.Dto.Search;
using WWX.Model;

namespace WWX.Admin.DAL
{
    public class MemberDAL : BaseDAL, IMemberDAL
    {
        public MemberDAL(IUnitOfWork uow) : base(uow)
        {
        }

        public void Add(Member model)
        {
            var sql = "insert into members(id,account,nickname,truename,phone,pwd,cash,zcash,remark,createdtime,createdby,updatedtime,updatedby,isactive,registerdate,address) values(?id,?account,?nickname,?truename,?phone,?pwd,?cash,?zcash,?remark,?createdtime,?createdby,?updatedtime,?updatedby,?isactive,?registerdate,?address)";
            var parameters = new Dictionary<string, object>();
            parameters.Add("id", model.Id);
            parameters.Add("account", model.Account);
            parameters.Add("nickname", model.NickName);
            parameters.Add("truename", model.TrueName);
            parameters.Add("phone", model.Phone);
            parameters.Add("cash", model.Cash);
            parameters.Add("zcash", model.ZCash);
            parameters.Add("registerdate", model.RegisterDate);
            parameters.Add("remark", model.Remark);
            parameters.Add("createdtime", model.CreatedTime);
            parameters.Add("createdby", model.CreatedBy);
            parameters.Add("updatedtime", model.UpdatedTime);
            parameters.Add("updatedby", model.UpdatedBy);
            parameters.Add("isactive", model.IsActive);
            parameters.Add("address", model.Address);
            parameters.Add("pwd", model.Pwd);
            

            this.Execute(sql, parameters);
        }

        public List<Member> GetPageList(MemberAdminSearch search, int pageNo, int pageSize, out int total)
        {
            List<Member> retList = null;
            Dictionary<string, object> parameters = null;
            var whereSql = GenerateWhereSql(search, out parameters);

            string totalSql = "select count(1) from members where 1=1 " + whereSql;
            total = this.GetCount(totalSql, parameters);
            if (total != 0)
            {
                var pagingSql = "select * from members where 1=1 " + whereSql + " order by createdtime desc limit " + (pageNo - 1) * pageSize + "," + pageSize;

                retList = this.GetModels<Member>(pagingSql, parameters);

            }

            return retList ?? new List<Member>();
        }

        private string GenerateWhereSql(MemberAdminSearch search, out Dictionary<string, object> parameters)
        {
            parameters = this.CreateParameters();
            string sql = "";

            if (search.EndDate.HasValue)
            {
                sql += " and registerdate<'" + search.EndDate.Value.AddDays(1).Date.ToString() + "'";
            }

            if (search.StartDate.HasValue)
            {
                sql += " and registerdate>='" + search.StartDate.Value.Date.ToString() + "'";
            }

            if (search.Account.IsNotNullOrWhiteSpace())
            {
                sql += " and account like ?account";
                parameters.Add("account", "%" + search.Account.Trim() + "%");
            }

            if (search.Phone.IsNotNullOrWhiteSpace())
            {
                sql += " and phone like ?phone";
                parameters.Add("phone", "%" + search.Phone.Trim() + "%");
            }

            if (search.TrueName.IsNotNullOrWhiteSpace())
            {
                sql += " and truename like ?truename";
                parameters.Add("truename", "%" + search.TrueName.Trim() + "%");
            }

            if (search.NickName.IsNotNullOrWhiteSpace())
            {
                sql += " and truename like ?truename";
                parameters.Add("truename", "%" + search.NickName.Trim() + "%");
            }

            if (search.Remark.IsNotNullOrWhiteSpace())
            {
                sql += " and remark like ?remark";
                parameters.Add("remark", "%" + search.Remark.Trim() + "%");
            }


            return sql;
        }
        public void Update(Member model)
        {
            var sql = "update members set address=?address, nickname=?nickname,truename=?truename,phone=?phone,remark=?remark,updatedtime=?updatedtime,updatedby=?updatedby, isactive=?isactive,address=?address  where id=?id";
            var parameters = new Dictionary<string, object>();
            parameters.Add("address",model.Address);
            parameters.Add("nickname", model.NickName);
            parameters.Add("truename", model.TrueName);
            parameters.Add("phone", model.Phone);
            parameters.Add("isactive", model.IsActive);
            parameters.Add("updatedtime", model.UpdatedTime);
            parameters.Add("updatedby", model.UpdatedBy);
            parameters.Add("id", model.Id);
            parameters.Add("remark", model.Remark);
            this.Execute(sql, parameters);
        }

        public void UpdateCash(string id, decimal cash, decimal zcash)
        {
            var sql = "update members set cash=?cash, zcash=?zcash where id=?id";
            var parameters = new Dictionary<string, object>();
            parameters.Add("cash", cash);
            parameters.Add("zcash", zcash);
            parameters.Add("id", id);

            this.Execute(sql, parameters);
        }

        public Member Get(string id)
        {
            var sql = "select * from members where id=?id";
            var parameters = new Dictionary<string, object>();
            parameters.Add("id", id);

            return this.GetModel<Member>(sql, parameters);
        }

        public Member GetByAccount(string account)
        {
            var sql = "select * from members where account=?account";
            var parameters = new Dictionary<string, object>();
            parameters.Add("account", account);

            return this.GetModel<Member>(sql, parameters);
        }

        public void UpdatePwd(string id, string pwd)
        {
            var sql = "update members set pwd=?pwd where id=?id";
            var parameters = new Dictionary<string, object>();
            parameters.Add("pwd", pwd);
            parameters.Add("id", id);

            this.Execute(sql, parameters);
        }
    }
}
